import UIKit

var str = "ABCDEF"

str.count
str.startIndex
str.endIndex

// before
// end
str[str.startIndex] // A
str[str.index(before: str.endIndex)] // F
str[str.index(str.startIndex, offsetBy: 3)] // D

var index1 = str.index(str.startIndex, offsetBy: 2) // 2
var index2 = str.index(str.startIndex, offsetBy: 4) // 4
str[index1 ... index2] // 2...4    CDE

// 第一次出现的位置
var c: String.Index = str.firstIndex(of: "E") ?? str.endIndex
str[str.startIndex ... c] // ABCDE

str.prefix(2)

let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
// 如果最大长度值超过数组长度,则返回所有值
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"

// 从后往前取值
let reversedIndex = str.index(str.endIndex, offsetBy: -3)
str[reversedIndex ..< str.endIndex] // DEF
str[reversedIndex...] // DEF
let res = str[reversedIndex...] // DEF
String(res.reversed()) // 反转

str.contains("CD")
str.contains("F")

str.hasPrefix("A")
str.hasSuffix("DEF")
str.hasSuffix("EF")

// 增
str.append(contentsOf: "G")

str.append("H")
str.insert(contentsOf: "hello", at: str.index(str.startIndex, offsetBy: 3))

// 替换
let id1 = str.index(str.startIndex, offsetBy: 1)
let id2 = str.index(str.startIndex, offsetBy: 2)
let range1 = id1 ... id2
str.replaceSubrange(range1, with: "替换")

// 删除
var str1 = "ABCDEF"
let r1 = str1.index(str1.startIndex, offsetBy: 1)
let r2 = str1.index(str1.startIndex, offsetBy: 3)
str1.removeSubrange(r1 ..< r2)
str1

for item in str1 {
  print(item)
}

print("====索引====")
//使用索引
for index in 0..<str.count {
  print(str[str.index(str.startIndex, offsetBy: index)])
}

print("---多行字符串---")
//多行字符
var multi = """
a
  b
    c
"""
print(multi)


//#""#中的所有特殊字符不做转义处理
/*
 也可以使用多行形式#"""
"""#
*/
let nonescape = #"""
%$#@#%@#%@#"/
k
  sdf
        森扥萨芬
"""#
print(nonescape)
